﻿
"use strict";
var ticketingApp = (function () {
    //var ticketingApp = {};

    var tickets = [];
    var firebaseUrl = "https://nicksticketing.firebaseio.com/.json";

    var loadTickets = function () {
        $.ajax({
            url: firebaseUrl,
            type: 'GET',

        }).done(function (data) {
            for (var i in data){
                var ticket = data[i];
                tickets.push(ticket);
            };

        });



     


    };


    var CreateTicket = function (issue, priority, name) {
        this.issue = issue;
        this.priority = priority;
        this.name = name;
        this.date = new Date();
    };

    var displayTickets = function () {
        var h = '<table class="table  table-striped">'
        h += '<tr>';
        h += '<th>Issue</th>';
        h += '<th>Priority</th>';
        h += '<th>Name</th>';
        h += '<th>Date</th>';
        h += '</tr>';

        for (var i in tickets) {
            if (!tickets.hasOwnProperty(i)) {
                continue;
            }
            var t = tickets[i];

           
            h += '<tr>';
            h += '<td>' + t.issue + '</td>';
            h += '<td>' + t.priority + '</td>';
            h += '<td>' + t.name + '</td>';
            h += '<td>' + t.date + '</td>';
            h += '</tr>';
            h += '</table>';
        }
        document.getElementById('tickets').innerHTML = h;
    };
    var sendTicket = function () {

        var issue = document.getElementById('issue').value;
        var priority = document.getElementById('priority').value;
        var name = document.getElementById('name').value;

        var ticket = new CreateTicket(issue, priority, name);
       

        

        $.ajax({
            url: firebaseUrl,
            type: 'POST',
            data: JSON.stringify(ticket),
            contentType: 'application/json',
            dataType: 'text'

        }).done(function (data) {
            alert(data.name);
            tickets.push(ticket);
            displayTickets();
        });


        
    };

    loadTickets();

    return {
        sendTicket: sendTicket,
        displayTickets: displayTickets
    };
})();




